<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>智慧室控平台-主页</title>
    </head>
    <body>
        <a href="login.html" style="text-decoration: none;">智慧室控平台-主页</a>
        <div>
            <b>D0灯</b>
            <button onclick="openOrCloseMQTT(this, 'led1')">开</button>
        </div>
        <div>
            <b>D4灯</b>
            <button onclick="openOrCloseMQTT(this, 'led2')">开</button>
        </div>
        <div>
            <b>温度</b><i>20</i>
            <b>湿度</b><i>30</i>
            <button>开</button>
        </div>
        
        <script>
            function openOrCloseMQTT(btn, sensor_name){
                let status = btn.innerText=="开"?"关":"开";
               
                let cmd = sensor_name+":"+(btn.innerText=="开"?"on":"off");
                console.log(cmd)
                // 请求后台cgi，发布mqtt的指令
                // url: /cgi-bin/led_mqtt?msg=led1:on
                fetch(" /cgi-bin/led_mqtt?msg="+cmd).then(resp=>resp.json()).then(data=>{
                    alert(data.msg);
                })
                btn.innerText = status;
            }
        </script>
    </body>
</html>